//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "RelativeDateRateHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Yield/RateHelper.h>
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::CRelativeDateRateHelper (boost::shared_ptr<QuantLib::RelativeDateRateHelper>& childNative, Object^ owner) : CRateHelper(CRelativeDateRateHelper::typeid)
{
#ifdef HANDLE
	_phRelativeDateRateHelper = NULL;
#endif
	_ppRelativeDateRateHelper = &childNative;
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppRelativeDateRateHelper));
}
Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::CRelativeDateRateHelper (QuantLib::RelativeDateRateHelper& childNative, Object^ owner) : CRateHelper(CRelativeDateRateHelper::typeid)
{
#ifdef HANDLE
	_phRelativeDateRateHelper = NULL;
#endif
	_ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (&childNative);
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppRelativeDateRateHelper));
    _RelativeDateRateHelperOwner = owner;
    _RateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::CRelativeDateRateHelper (CRelativeDateRateHelper^ copy) : CRateHelper(CRelativeDateRateHelper::typeid)
{
#ifdef HANDLE
	_phRelativeDateRateHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (copy->GetShared());
        _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppRelativeDateRateHelper));
    }
}
Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::CRelativeDateRateHelper (System::Type^ t) : CRateHelper(CRelativeDateRateHelper::typeid)
{
#ifdef HANDLE
	_phRelativeDateRateHelper = NULL;
#endif
	if (!t->IsSubclassOf(CRelativeDateRateHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::CRelativeDateRateHelper (QuantLib::Handle<QuantLib::RelativeDateRateHelper>& childNative, Object^ owner)  : CRateHelper(CRelativeDateRateHelper::typeid)
{
	_phRelativeDateRateHelper = &childNative;
	_ppRelativeDateRateHelper = &static_cast<boost::shared_ptr<QuantLib::RelativeDateRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppRelativeDateRateHelper));
    _RelativeDateRateHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::CRelativeDateRateHelper (QuantLib::Handle<QuantLib::RelativeDateRateHelper> childNative)  : CRateHelper(CRelativeDateRateHelper::typeid)
{
	_phRelativeDateRateHelper = &childNative;
	_ppRelativeDateRateHelper = &static_cast<boost::shared_ptr<QuantLib::RelativeDateRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppRelativeDateRateHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::CRelativeDateRateHelper (QuantLib::RelativeDateRateHelper childNative)  : CRateHelper(CRelativeDateRateHelper::typeid)
{
#ifdef HANDLE
	_phRelativeDateRateHelper = NULL;
#endif
	_ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (new QuantLib::RelativeDateRateHelper (childNative));
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppRelativeDateRateHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::~CRelativeDateRateHelper ()
{
    if (_ppRelativeDateRateHelper != NULL)
    {
	    delete _ppRelativeDateRateHelper;
        _ppRelativeDateRateHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::!CRelativeDateRateHelper ()
{
    if (_ppRelativeDateRateHelper != NULL)
    {
	    delete _ppRelativeDateRateHelper;
    }
}
QuantLib::RelativeDateRateHelper& Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::GetReference ()
{
    if (_ppRelativeDateRateHelper == NULL) throw gcnew NativeNullException ();
	return **_ppRelativeDateRateHelper;
}
boost::shared_ptr<QuantLib::RelativeDateRateHelper>& Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::GetShared ()
{
    if (_ppRelativeDateRateHelper == NULL) throw gcnew NativeNullException ();
	return *_ppRelativeDateRateHelper;
}
QuantLib::RelativeDateRateHelper* Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::GetPointer ()
{
    if (_ppRelativeDateRateHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppRelativeDateRateHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RelativeDateRateHelper>& Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::GetHandle ()
{
	if (_phRelativeDateRateHelper == NULL)
	{
		_phRelativeDateRateHelper = new Handle<QuantLib::RelativeDateRateHelper> (*_ppRelativeDateRateHelper);
	}
	return *_phRelativeDateRateHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::HasNative () 
{
	return (_ppRelativeDateRateHelper != NULL);
}

Cephei::QL::Termstructures::Yield::IRelativeDateRateHelper^ Cephei::QL::Termstructures::Yield::CRelativeDateRateHelper::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppRelativeDateRateHelper)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

